import { _decorator, animation, Animation, IVec2Like, AnimationClip, BoxCollider2D, Collider2D, Component, Contact2DType, EventKeyboard, geometry, Input, input, IPhysics2DContact, KeyCode, math, Node, PhysicsSystem, PhysicsSystem2D, RigidBody2D, size, UITransform, Vec2, Vec3, ERaycast2DType, director, Sprite } from 'cc';
import { GameManager } from './GameManager';
import { CameraControl } from './CameraControl';
const { ccclass, property } = _decorator;

@ccclass('PlayerControl')
export class PlayerControl extends Component {
  direction: number = 1
  isMove: boolean = false
  @property({
    type: Number
  })
  speed: number = 200
  flashSpeed: number = 1000
  flashTime: number = 200 // 闪现时间毫秒
  @property(Number)
  jumpPower = 10
  isJump: boolean = false
  animationComponent: Animation = null
  isAttack: boolean = false
  isFlash: boolean = false
  clips: AnimationClip[] = []
  manager: Node = null
  max: number = null
  min: number = null
  flashLight: Node = null
  onLoad() {
    this.flashLight = this.node.getChildByName('flash')
    this.max = this.node.getParent().getChildByName('Camera').getComponent(CameraControl).max + 400
    this.min = this.node.getParent().getChildByName('Camera').getComponent(CameraControl).min - 400
    input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
    input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
    this.animationComponent = this.node.getComponent(Animation)
    this.clips = this.animationComponent.clips
    this.manager = this.node.getParent().getChildByName('manager')
  }
  start() {
    this.getComponent(BoxCollider2D).on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this)
  }
  init(params: any) {
    console.log(params);
  }
  onKeyDown(e: EventKeyboard) {
    if (e.keyCode === KeyCode.KEY_W) {
      if (this.isJump) return
      if (this.isAttack) return
      if (this.isFlash) return
      this.isJump = true
      this.animationComponent.defaultClip = this.clips[2]
      this.animationComponent.play()
      this.schedule(() => {
        if (!this.isMove) {
          this.animationComponent.defaultClip = this.clips[0]
          this.animationComponent.play()
        } else {
          this.animationComponent.defaultClip = this.clips[1]
          this.animationComponent.play()
        }
      }, 0, 1, 0.6)
      this.getComponent(RigidBody2D).linearVelocity = new Vec2(0, this.jumpPower)
    }
    if (e.keyCode === KeyCode.KEY_A) {
      if (this.isAttack) return
      if(this.isFlash) return
      this.animationComponent.defaultClip = this.clips[1]
      this.animationComponent.play()
      this.manager.getComponent(GameManager).runVoice(true)

      this.isMove = true
      this.direction = -1

    }
    if (e.keyCode === KeyCode.KEY_D) {
      if (this.isAttack) return
      if (this.isFlash) return
      this.animationComponent.defaultClip = this.clips[1]
      this.animationComponent.play()
      this.manager.getComponent(GameManager).runVoice(true)

      this.isMove = true
      this.direction = 1
    }
    if (e.keyCode === KeyCode.KEY_J) {
      if (this.isAttack) return
      if (this.isFlash) return
      this.attack()
    }
    if (e.keyCode === KeyCode.KEY_L) {
      this.isFlash = true
    }
  }

  onKeyUp(e: EventKeyboard) {
    if (e.keyCode === KeyCode.KEY_A) {
      this.manager.getComponent(GameManager).runVoice(false)
      if (this.isAttack) return
      this.isMove = false
      this.animationComponent.defaultClip = this.clips[0]
      this.animationComponent.play()
    }
    if (e.keyCode === KeyCode.KEY_D) {
      this.manager.getComponent(GameManager).runVoice(false)
      if (this.isAttack) return
      this.isMove = false
      this.animationComponent.defaultClip = this.clips[0]
      this.animationComponent.play()
    }
  }

  move(dt: number) {
    const { x, y, z } = this.node.position
    this.node.setScale(new Vec3(this.direction, 1, 0))
    const moveX = this.direction === 1 ? x + this.speed * dt : x - this.speed * dt
    this.node.setPosition(new Vec3(moveX, y, z,))
  }
  flash(dt: number) {
    this.node.getComponent(Sprite).enabled = false
    this.flashLight.active = true //显示光效
    this.manager.getComponent(GameManager).flashVoice() // 播放声音
    setTimeout(() => {
      this.isFlash = false
      this.flashLight.active = false
      this.node.getComponent(Sprite).enabled = true
    }, this.flashTime)
    const { x, y, z } = this.node.position
    const moveX = this.direction === 1 ? x + this.flashSpeed * dt : x - this.flashSpeed * dt
    this.node.setPosition(new Vec3(moveX, y, z))
  }
  attack() {
    if (this.isJump) return
    this.isMove = false
    this.isAttack = true
    //设置UI高度偏移
    this.getComponent(UITransform).anchorY = 0.355
    //播放攻击动画
    this.animationComponent.defaultClip = this.clips[3]
    this.animationComponent.play()
    this.manager.getComponent(GameManager).kattackVoice()

    //清除攻击动画 重置UI高度偏移
    this.schedule(() => {
      this.getComponent(UITransform).anchorY = 0.5
      this.animationComponent.defaultClip = this.clips[0]
      this.animationComponent.play()
      this.isAttack = false
    }, 0, 1, 2.214)
  }
  //检测是否可以跳跃
  onBeginContact(self: Collider2D, other: Collider2D, contact: IPhysics2DContact) {
    if(other.tag===1){
      this.manager.getComponent(GameManager).hpCount-=0.2 // 中箭掉血
    }
    if (other.tag === 2) {
      this.isJump = false
    }
  }

  update(dt: number) {
    // 闪现时不能移动
    if (this.isMove) {
      this.move(dt)
    }
    if (this.isFlash) {
      this.flash(dt)
    }
    // const { x, y, z } = this.node.position
    // const skewX = x + 20
    // this.rayTest({ x, y }, { x: skewX, y })
  }

  onDestroy() {
    input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
    input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
  }

  //射线检测
  rayTest(p1: IVec2Like, p2: IVec2Like) {
    const res = PhysicsSystem2D.instance.raycast(p1, p2, ERaycast2DType.Closest)
    console.log(res);
  }
  die(){
    console.log('die');
    
    this.isMove = false
    this.isFlash = false
    // 播放死亡动画
    // 播放音效
    // 切换死亡场景
  }
}






